package com.mask.roll.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.edu.entity.Chapter;
import com.mask.roll.edu.entity.VO.chapter.ChapterVO;
import com.mask.roll.edu.entity.VO.chapter.VideoVO;
import com.mask.roll.edu.entity.Video;
import com.mask.roll.edu.mapper.ChapterMapper;
import com.mask.roll.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.roll.edu.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hx
 * @since 2021-10-23
 */
@Service
@Slf4j
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        //根据课程id查询所有章节
        QueryWrapper<Chapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<Chapter> chapterList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询所有小节
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<Video> videoList = videoService.list(wrapperVideo);

        List<ChapterVO> finalList = new ArrayList<>();

        //遍历章节list封装
        for (int i = 0; i < chapterList.size(); i++) {
            Chapter chapter = chapterList.get(i);
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter,chapterVO);
            finalList.add(chapterVO);

            ArrayList<VideoVO> videoVOList = new ArrayList<>();
            //遍历小节封装
            for (int j = 0; j < videoList.size(); j++) {
                Video video = videoList.get(j);
                //判断小节的chapterId和章节的id是否一致
                if (video.getChapterId().equals(chapter.getId())){
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(video,videoVO);
                    videoVOList.add(videoVO);
                }
            }

            //小节list封装到章节中
            chapterVO.setChildren(videoVOList);
        }

        return finalList;
    }

    @Override
    public void deleteChapter(String chapterId) {
        //查询章节下是否有小节
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(videoWrapper);
        if (count > 0) {
            log.warn("删除章节{}失败,该章节下有数据",chapterId);
            throw new RollException(ServiceEnums.CHAPTER_EXISTS_ERROR.getCode(),
                    RollConstants.ERROR,ServiceEnums.CHAPTER_EXISTS_ERROR.getMessage());
        }else {
            try {
                baseMapper.deleteById(chapterId);
                log.info("删除章节{}成功",chapterId);
            } catch (Exception e) {
                log.error("删除章节{}失败:{}",chapterId,e.getMessage());
                throw new RollException(ServiceEnums.CHAPTER_DELETE_ERROR.getCode(),
                        RollConstants.ERROR,ServiceEnums.CHAPTER_DELETE_ERROR.getMessage());
            }
        }

    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
